<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 随机数 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace common;


class Random {

    /**
     * 生成随机串
     *
     * @param int $length 随机串长度
     * @param bool $includeChar 是否包含字母
     * @param bool $includeNumber 是否包含数字
     * @param bool $includePunctuation 是否包含标点
     * @param string $customDict 自定义字典
     * @return string
     */
    public static function get($length = 8, $includeChar = true, $includeNumber = true, $includePunctuation = false, $customDict = '') {
        // 密码字符集
        $dict = '';
        if ($includeChar) {
            $dict .= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        }
        if ($includeNumber) {
            $dict .= '0123456789';
        }
        if ($includePunctuation) {
            $dict .= '!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
        }
        $dict .= $customDict;
        //生成随机串
        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= $dict[mt_rand(0, strlen($dict) - 1)];
        }
        return $str;
    }

    /**
     * 生成uuid
     *
     * @param string $hyphen
     * @return string
     */
    public static function uuid($hyphen = "") {
        mt_srand((double)microtime() * 10000);
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $uuid =
            substr($charid, 0, 8) . $hyphen
            . substr($charid, 8, 4) . $hyphen
            . substr($charid, 12, 4) . $hyphen
            . substr($charid, 16, 4) . $hyphen
            . substr($charid, 20, 12);
        return $uuid;
    }

}